//
//  AddImageCell.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/9/7.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "AddImageCell.h"
#import <UIButton+WebCache.h>

@interface AddImageCell()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageHeightCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageWidthCons;
@property (nonatomic,strong) NSMutableArray *btnsArr;
@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet AddImageButton *firstBtn;

@end
@implementation AddImageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.imageWidthCons.constant = ImageWidth;
    self.imageHeightCons.constant = ImageWidth;
    [self.btnsArr addObject:self.firstBtn];
    [self addImageWithBtn:self.firstBtn];
    [self.firstBtn addTarget:self action:@selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)addImage:(AddImageButton *)btn
{
    if (![btn.currentBackgroundImage.accessibilityIdentifier isEqualToString:@"btn_upload_image_normal"]) {
        return;
    }
    if (self.addImageBlock) {
        self.addImageBlock(self.type,btn);
    }
}
- (void)setModel:(AddGoodsModel *)model
{
    _model = model;
    switch (self.type) {
        case AddImageCellTypeBanners:
        {
            self.nameLab.text = @"商品图片";
            [self detailBanners:self.model.productBannerList];
        }
            break;
        case AddImageCellTypeGoodsDetail:
        {
            self.nameLab.text = @"商品详情";
            [self detailBanners:self.model.productDetailList];
        }
            break;
        case AddImageCellTypeActivity:
        {
            self.nameLab.text = @"活动图片";
            [self detailActicityBanner:self.model.bannerList];
        }
            break;
        default:
            break;
    }
}
//外面取消重用 btn数组都是新创建的 里面不会有值
- (void)detailBanners:(NSMutableArray *)arr
{
    for (NSInteger i=0; i<arr.count; i++) {
        AddImageButton *btn = [[AddImageButton alloc]init];
        [self.btnsArr addObject:btn];
        [self.contentView addSubview:btn];
        [btn addTarget:self action:@selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
    }
    __WEAK
    for (NSInteger i=0; i<self.btnsArr.count; i++) {
        AddImageButton *btn = self.btnsArr[i];
        if (i<arr.count) {
            btn.imageStr = arr[i];
            [btn sd_setBackgroundImageWithURL:[NSURL URLWithString:arr[i]] forState:UIControlStateNormal];
            btn.deleteBlock = ^(AddImageButton *btn){
                [weakSelf.btnsArr removeObject:btn];
                [arr removeObject:btn.imageStr];
                [btn removeFromSuperview];
                [weakSelf layoutSubviews];
                if (weakSelf.deleteBlock) {
                    weakSelf.deleteBlock(self.type);
                }
            };
        }else{
            [self addImageWithBtn:btn];
        }
    }
}
- (void)addImageWithBtn:(UIButton *)btn
{
    UIImage *image = GetImage(@"btn_upload_image_normal");
    //设置image的标识符
    [image setAccessibilityIdentifier:@"btn_upload_image_normal"];
    [btn setBackgroundImage:image forState:UIControlStateNormal];
}
- (void)detailActicityBanner:(NSMutableArray *)arr
{
    for (NSInteger i=0; i<arr.count; i++) {
        if (i>=2) {
            continue;
        }
        AddImageButton *btn = [[AddImageButton alloc]init];
        [self.btnsArr addObject:btn];
        [self.contentView addSubview:btn];
        [btn addTarget:self action:@selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
    }
    __WEAK
    for (NSInteger i=0; i<self.btnsArr.count; i++) {
        AddImageButton *btn = self.btnsArr[i];
        if (i<arr.count) {
            btn.imageStr = arr[i];
            [btn sd_setBackgroundImageWithURL:[NSURL URLWithString:arr[i]] forState:UIControlStateNormal];
            btn.deleteBlock = ^(AddImageButton *btn){
                [weakSelf.btnsArr removeObject:btn];
                [arr removeObject:btn.imageStr];
                [btn removeFromSuperview];
                [weakSelf layoutSubviews];
                if (weakSelf.deleteBlock) {
                    weakSelf.deleteBlock(self.type);
                }
            };
        }else{
            [self addImageWithBtn:btn];
        }
    }
}

- (NSMutableArray *)btnsArr
{
    if (!_btnsArr) {
        _btnsArr = [NSMutableArray array];
    }
    return _btnsArr;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    for (NSInteger i=0; i<self.btnsArr.count; i++) {
        AddImageButton *btn = self.btnsArr[i];
        NSInteger row = i/4;
        NSInteger line = i%4;
        btn.frame = CGRectMake(10+(ImageWidth+20)*line, 38+(ImageWidth+10)*row, ImageWidth, ImageWidth);
    }
}

@end
